package com.situ.mall.controller.back;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Category;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Result;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	@Autowired
	private IProductService productService;
	
//*******获取 添加     商品分类的二级分类 category' service&mapper层 获取 一级和二级分类********
	// 后台4.添加： 从category获得一级分类，转发insert.jsp
	@Autowired
	private ICategoryService categoryService;
//*******获取 添加     商品分类的二级分类 category' service&mapper层********
	
	@RequestMapping("/selectAll.action")
	@ResponseBody
	public List<Product> selectAll() {
		System.out.println("ProductController.selectAll()");
		return productService.selectAll();
	}
	

// 0. index.jsp点一下   跳转到这里    再转发到   product_list。jsp
	@RequestMapping("/getProductListJsp.action")
	public String getProductListJsp() {
		System.out.println("ProductController.getProductListJsp()");
		return "product_list";
	}
// 1.查询 每页信息     返回Result到	product_list.jsp
	@RequestMapping("/selectByPage.action")
	@ResponseBody
	public Result selectByPage( @RequestParam(defaultValue = "1")Integer page,  
								@RequestParam(defaultValue = "5")Integer limit, 
								Product product) {
		System.out.println("ProductController.selectByPage()");
		System.out.println(product);
		
		return productService.selectByPage(page,limit,product);
	}
	
// 2.批量删除
	@RequestMapping("/deleteAll.action")
	@ResponseBody
	public Result deleteAll(Integer[] ids) {  
		// 批量删除   好多个  所以接收数组
		System.out.println("ProductController.deleteAll()");
		System.out.println(Arrays.toString(ids));
		
		boolean isSuccess = productService.deleteAll(ids);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}
	
// 3.删除
	@RequestMapping("/deleteById.action")
	@ResponseBody
	public Result deleteById(Integer id) {
		System.out.println("StudentController.deleteById()");
		System.out.println("id="+id);
		
		boolean isSuccess = productService.deleteById(id);
		if (isSuccess) {
			System.out.println("删除成功");
			return Result.success("删除成功");
		}else {
			System.out.println("删除失败");
			return Result.error("删除失败");
		}
	}
	
// 4.添加  
	//4.1.获取一级联动(商品分类)   从category的service mapper获取   转发到insert.jsp
	@RequestMapping("/getProductInsertPage.action")
	public String getProductInsertPage(Model model) {
		System.out.println("ProductController.getProductInsertPage()");
		List<Category> topCategoryList = categoryService.getTopCategoryList();     //*************  Category的sevice mapper 获取一级联动信息
		System.out.println(topCategoryList);
		model.addAttribute("topCategoryList",topCategoryList);
		return "product_insert"; //视图解析器springmvc_back
	}
	
	//4.2 添加 进数据库 
	@RequestMapping("/insert.action")
	@ResponseBody
	public Result insert(Product product) {
		System.out.println("ProductController.insert()");
		System.out.println("product="+product);
		
		boolean isSuccess = productService.insert(product);
		if (isSuccess) {
			System.out.println("添加成功");
			return Result.success("添加成功");
		}else {
			System.out.println("添加失败");
			return Result.error("添加失败");
		}
	}
	
}
